package com.easy.controller;


import com.easy.bean.PetBean;
import com.easy.service.PetBeanService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("pet")
public class PetController {
    @Autowired
    PetBeanService petBeanService;

    @PostMapping
    public Result add(@RequestBody PetBean petBean){
        Object obj=petBeanService.add(petBean);
        return Result.success(obj);
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id){
        int obj=petBeanService.del(id);
        return Result.success(obj);
    }

    @PutMapping
    public Result edit(@RequestBody PetBean petBean){
        Object obj=petBeanService.edit(petBean);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(PetBean petBean){
        Object obj=petBeanService.all(petBean);
        return Result.success(obj);
    }

    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id){
        Object obj=petBeanService.one(id);
        return Result.success(obj);
    }

    @GetMapping("page")
    public Result page(Page page, PetBean petBean){
        petBeanService.page(page,petBean);
        return Result.success(page);
    }

    // 统计宠物类型数量的接口（GET请求）
    @GetMapping("countKind")
    public Result countPetKind() {
        // 调用Service层获取统计数据
        Map<String, Integer> data = petBeanService.countPetKind();
        // 使用统一Result工具类返回成功响应
        return Result.success("宠物类型统计成功", data);
    }

}
